$(document).ready(function() {
    var paths = $("#map_canvas").data("m");
    var blank = $("#map_canvas").data("b");

    var jsonData = $.ajax({
        type: "POST",
        url: "queryPoints.php",
        data: { "m": paths },
        dataType: "json"
    }).done(function (data, status, xhr) {
        if (data.status !== "success") {
            console.log("queryPoints.php did not return success");
            return false;
        }

        var myOptions = {
            zoom: 12,
            center: new google.maps.LatLng(data.startLat, data.startLon),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scaleControl: true,
            overviewMapControl: true,
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID],
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            }
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        trimble.myTopo.init(map);
        map.setMapTypeId(trimble.myTopo.MapTypeId.Topo);
        colorWheel = [];
        
        if (data.data.mapType = "tracks") {

            for (var trackIndex in data.data) {
                var color = colorWheel.pop();
                if (!color) {
                    colorWheel = ["#FFFFFF", "#00FFFF", "#FF00FF", "#FFFF00", "#0000FF", "#FF0000", "#00FF00"];
                    var color = colorWheel.pop();
                }
                var track = data.data[trackIndex];
                var trackID = track.trackID;
                var trackData = track.trackData;
                var points = trackData.points;
                var startLat = trackData.startLat;
                var startLon = trackData.startLon;
                var startDate = trackData.startDate;
                var startElevation = trackData.startElevation;
                var endLat = trackData.endLat;
                var endLon = trackData.endLon;
                var endDate = trackData.endDate;
                var endElevation = trackData.endElevation;
                var points = trackData.points;

                var pointList = [];
                for (i in points) {
                    pointList.push(new google.maps.LatLng(points[i].lat, points[i].lon));
                }

                var flightPath = new google.maps.Polyline({
                    path: pointList,
                    strokeColor: color,
                    strokeOpacity: 1.0,
                    strokeWeight: 4
                });

                flightPath.setMap(map);
                var startLatLng = new google.maps.LatLng(startLat, startLon);
                var endLatLng = new google.maps.LatLng(endLat, endLon);

                var startMarker = new google.maps.Marker({
                    position: startLatLng,
                    map: map,
                    title: startDate
                });

                var endMarker = new google.maps.Marker({
                    position: endLatLng,
                    map: map,
                    title: endDate
                });
            }
        } else if (data.data.mapType = "centerOnPoint") {
            var color = "#00FF00";
        }
    });
});
